﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;


public partial class GoToLocation : System.Web.UI.UserControl
{
    public string m_mapId = "Map1";
    public string m_scriptUrl = "GoToLocation/GoToLocation.js";
    private Map m_map;

    protected void Page_Load(object sender, EventArgs e)
    {
        // find the map control
        if (m_mapId == null || m_mapId.Length == 0) m_mapId = "Map1";
        m_map = Page.FindControl(m_mapId) as Map;
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (!m_map.IsAsync)
        {
            if (!Page.ClientScript.IsStartupScriptRegistered("goToLocationScript"))
            {
                string script = String.Format("<script language='javascript' type='text/javascript' src='{0}'></script>\n", m_scriptUrl);
                Page.ClientScript.RegisterStartupScript(typeof(Page), "goToLocationScript", script);
            }

            if (!Page.ClientScript.IsStartupScriptRegistered(ClientID + "_InitScript"))
            {
                string parentPanelString = ParentPanel == null ? "'null'" : "'" + ParentPanel + "'";
                string initScript = @"
                                Sys.Application.add_init(function() 
                                {{ 
                                    $create(ADF.Samples.goToLocationClass,{{'panelID': {0}}},null,{{'map': '{1}'}},$get('{2}'));
                                }});
                                ";

                initScript = String.Format(initScript, parentPanelString, m_mapId, ClientID);
                Page.ClientScript.RegisterStartupScript(typeof(Page), ClientID + "_InitScript", initScript, true);
            }
        }
    }

    #region Properties

    /// <summary>
    /// Id of Buddy MapControl
    /// </summary>
    [DefaultValue("Map1")]
    [Description("ID of Map control.")]
    public string Map
    {
        get { return m_mapId; }
        set { m_mapId = value; }
    }

    [Browsable(false)]
    public string ParentPanel
    {
        get
        {
            if (Parent is FloatingPanel) 
                return Parent.ClientID;
            else
                return null;
        }
    }

    /// Path to javascript file
    /// </summary>
    [DefaultValue("GoToLocation.js")]
    [Description("Url of javascript file")]
    public string ScriptUrl
    {
        get { return m_scriptUrl; }
        set { m_scriptUrl = value; }
    }

    #region Non-Browsable Properties
    [Browsable(false)]
    public override bool Visible
    {
        get { return base.Visible; }
        set { base.Visible = value; }
    }
    [Browsable(false)]
    public override bool EnableTheming
    {
        get { return base.EnableTheming; }
        set { base.EnableTheming = value; }
    }
    [Browsable(false)]
    public override bool EnableViewState
    {
        get { return base.EnableViewState; }
        set { base.EnableViewState = value; }
    }
    #endregion
    #endregion
}
